package com.example.java.serializable;

import java.io.ObjectStreamException;
import java.io.Serializable;

public class Orientation implements Serializable {

    public static final Orientation HORIZONTAL = new Orientation(1);
    public static final Orientation VERTICAL = new Orientation(2);

    private int value;

    private Orientation(int value) {
        this.value = value;
    }

    /**
     *定义这个方法,在对象被序列化之后就会调用它.它必须返回一个对象,而返回的对象之后会成为
     * readObject的返回值. 它可以解决序列化单例或安全枚举类型后对象不一致的问题.
     * @return
     */
    protected Object readResolve() throws ObjectStreamException{
        if (value == 1) {
            return Orientation.HORIZONTAL;
        }
        if (value == 2) {
            return Orientation.VERTICAL;
        }
        return null;
    }
}
